﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DLL;

public partial class enTXDesign : System.Web.UI.Page
{
    private static string examNum = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["examNum"] != null)
        {
            examNum = Request.QueryString["examNum"];
            labCurrentExam.Text=Request.QueryString["examName"];
        }
        if (!IsPostBack)
        {
            BindData();            
        }
        viewIsNoData();
    }
    protected void gvTX_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        bool bl = examDesign_BLL.DeleteEnType(gvTX.DataKeys[e.RowIndex][0].ToString());
        if (bl)
        {
            ShowMessage("删除成功！");
            //刷新当前页
            RefurPage();
            //绑定题型数据源
            //BindData();
        }
        else
            ShowMessage("删除失败！");
    }
    protected void gvTX_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvTX.PageIndex = e.NewPageIndex;
        //绑定题型数据源
        BindData();
    }
    protected void gvTX_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvTX.EditIndex = e.NewEditIndex;
        //绑定题型数据源
        BindData();
    }

    //绑定题型数据源
    private void BindData()
    {
        object obj = examDesign_BLL.GetTXSource(examNum);
        if (obj != null)
        {
            gvTX.DataSource = obj;
            gvTX.DataBind();
        }
    }   

    //提交
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (txtCount.Text.Length == 0 || txtFullScore.Text.Length == 0 || txtSingleScore.Text.Length == 0)
        {
            ShowMessage("必要信息未填写！");
            return;
        }
        TX newType = new TX();
        newType.txName = dropTX.SelectedValue;
        newType.examNum = decimal.Parse(examNum);
        newType.fullScore = float.Parse(txtFullScore.Text);
        newType.singleScore = float.Parse(txtSingleScore.Text);
        newType.tl = int.Parse(txtCount.Text);
        newType.bz = txtBZ.Text==""?"无":txtBZ.Text;
        newType.timeArea = int.Parse(txtTimeLimit.Text==""?"0":txtTimeLimit.Text);
        if (btnSubmit.Text == "添加")
        {
            bool bl = examDesign_BLL.Add(newType);
            if (bl)
            {
                ShowMessage("添加成功！");
                //绑定题型数据源
                //刷新当前页
                RefurPage();
                //BindData();
            }
            else
                ShowMessage("添加失败！");
        }
        else
        {
            bool b = examDesign_BLL.Update(seleNum.ToString(), newType);
            if (b)
            {
                ShowMessage("更新成功！");
                //绑定题型数据源
                BindData();
                btnCancel_Click(sender, e);
            }
            else
                ShowMessage("更新失败！");
        }
    }
    //选择
    private static decimal seleNum = -1;
    protected void gvTX_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridViewRow row = gvTX.Rows[e.NewSelectedIndex];
        seleNum = (decimal)gvTX.DataKeys[e.NewSelectedIndex][0];
        txtBZ.Text=row.Cells[6].Text;
        txtCount.Text=row.Cells[4].Text;
        txtSingleScore.Text=row.Cells[3].Text;
        txtTimeLimit.Text=row.Cells[5].Text;
        txtFullScore.Text=row.Cells[2].Text;
        dropTX.ClearSelection();
        dropTX.Items.FindByText(row.Cells[1].Text).Selected = true;
        btnSubmit.Text = "更新";
    }
    //取消
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtBZ.Text = txtCount.Text = txtSingleScore.Text = txtTimeLimit.Text = txtFullScore.Text = "";
        dropTX.ClearSelection();
        btnSubmit.Text = "添加";
    }

    #region 消息

    //刷新当前页
    private void RefurPage()
    {
        RegisterStartupScript("refur", "<script type='text/javascript' languane='javascript'> location.replace(location.href);</script>");
    }

    /// <summary>
    /// 显示消息
    /// </summary>
    /// <param name="msg">消息内容</param>
    protected void ShowMessage(string msg)
    {
        RegisterStartupScript("msg", "<script type='text/javascript' language='javascript'>alert('" + msg + "')</script>");
    }
    /// <summary>
    /// 如果GridView中没有数据则显示提示信息
    /// </summary>
    protected void viewIsNoData()
    {
        if (gvTX.Rows.Count == 0)
        {
            RegisterStartupScript("prompt1", "<script type='text/javascript'>document.getElementById('divBrowse').innerHTML='(尚未添加相关数据)'</script>");
        }
    }
    #endregion
}
